use rand::{prelude::IndexedRandom, seq::SliceRandom};

pub fn process_genpass(
    opts: u8,
    no_upcase: bool,
    no_lowercase: bool,
    no_number: bool,
    no_symbol: bool,
) -> anyhow::Result<String> {
    let mut rng = rand::rng();
    let mut password = Vec::new();
    let mut chars = Vec::new();

    if !no_upcase {
        chars.extend_from_slice(b"ABCDEFGHJKLMNPQRSTUVWXYZ");
    }
    if !no_lowercase {
        chars.extend_from_slice(b"abcdefghijkmnpqrstuvwxyz");
    }
    if !no_number {
        chars.extend_from_slice(b"123456789");
    }
    if !no_symbol {
        chars.extend_from_slice(b"!@#$%^&*_");
    }

    for _ in 0..opts {
        let c = chars
            .choose(&mut rng)
            .expect("chars won't be empty in this context");
        password.push(*c);
    }

    password.shuffle(&mut rng);
    let password = String::from_utf8(password)?;
    Ok(password)
}
